<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Member_handle extends MY_Controller
{
    private $key = 'dad20285ad767b623f6fc4097628ac95';
    private $max_error_times = 5;
    private $password_lock_time = 10800;
    public function check()
    {
        // 加载会员辅助
        if (! is_file( $file = APPPATH . 'include/MemberHelper.class.php' ))
        {
            show_error( 'The class file MemberHelper.class.php does not exist.' );
        }
        include ($file);
        MySession::get_instance();
        if (MemberHelper::check_login( true ))
        {
            $this->load->helper( 'array' );
            $this->rs->ok( elements( array (
                    'username',
                    'picture' 
            ), $_SESSION['member'] ) );
        }
        $this->rs->err( '未登录' );
    }
    // 登陆操作
    public function login()
    {
        // 加载会员辅助
        if (! is_file( $file = APPPATH . 'include/MemberHelper.class.php' ))
        {
            show_error( 'The class file MemberHelper.class.php does not exist.' );
        }
        include ($file);
        
        // 获取登陆参数
        $username = $this->input->post( 'username' );
        $password = $this->input->post( 'password' );
        $remember = $this->input->post( 'remember' );
        
        if (empty( $username ))
        {
            $this->rs->err( '用户名不能为空' );
        }
        if (empty( $password ))
        {
            $this->rs->err( '密码不能为空' );
        }
        $result = MemberHelper::login( $username, $password, 1, $remember );
        if ($result === true)
        {
            $this->mylog->add( '登陆成功', 2 );
            $member = $_SESSION['member'];
            $this->rs->ok( array (
                    'sid' => session_id(),
                    'nickname' => $member['nickname'],
                    'picture' => $member['picture'],
                    'last_ip' => $member['last_ip'],
                    'last_time' => $member['last_time'] 
            ) );
        }
        else
        {
            $this->mylog->add( '登陆失败：' . $result . '（"' . $username . '"）', 1 );
            $this->rs->err( $result );
        }
    }
    // 退出登陆操作
    public function logout()
    {
        $this->mylog->add( '退出登陆', 2 );
        if (MemberHelper::logout())
        {
            $this->rs->ok();
        }
        else
        {
            $this->rs->err();
        }
    }
    function clean()
    {
        $id = ( int ) $this->input->post( 'id' );
        if (MemberHelper::clean( $id ))
        {
            $this->rs->ok();
        }
        $this->rs->err();
    }
    // 修改密码
    public function modify_password()
    {
        $old_password = $this->input->post( 'old_password' );
        $new_password = $this->input->post( 'new_password' );
        
        if ($old_password == $new_password)
        {
            $this->rs->err( '新密码不能与旧密码相同！' );
        }
        
        $uid = isset( $_SESSION['member'], $_SESSION['member']['id'] ) ? $_SESSION['member']['id'] : 0;
        
        if (MemberHelper::modify_password( $uid, $old_password, $new_password ) === 1)
        {
            $this->mylog->add( '修改密码成功', 2 );
            $this->rs->ok();
        }
        else
        {
            $this->mylog->add( '修改密码失败', 2 );
            $this->rs->err( '修改失败，请检查旧密码是否正确！' );
        }
    }
    // 注册
    public function register()
    {
        /*
        $username = $this->input->post( 'username' );
        $password = $this->input->post( 'password' );
        
        if (empty( $username ))
        {
            $rs->err( '用户名不能为空' );
        }
        
        $this->load->model( 'member_model' );
        $uid = $this->member_model->insert( array (
                'username' => $username,
                'password' => sha1( $this->key . $password ),
                'enable' => 1,
                'deleted' => 0,
                'nickname' => '新手',
                'add_time' => $_SERVER['REQUEST_TIME'] 
        ) );
        if ($uid)
        {
            
            $this->rs->ok( array (
                    'id' => $uid,
                    'username' => $username 
            ) );
        }
        $this->rs->err();
        */
    }
}
